//
//  EskLinePlot.m
//  CorePlotSample
//
//  Created by Ken Wong on 8/9/11.
//  Copyright 2011 Essence Work LLC. All rights reserved.
//


/*
 Status: changed
 Last Merge Date: 06-08-2012
 Last Change Date: 
 Changes:
 1. prior year animation
 2. ruller cannot be scrolled to far right
 
 */

#import "EskLinePlot.h"
#import "BalanceModel.h"
#import "Balance.h"

#define kBalancePlot @"BalancePlot" 
#define kBelowNinetyPlot @"BelowNinetyPlot" 
#define kAboveNinetyPlot @"AboveNinetyPlot" 
#define kRulerPlot @"RulerPlot"  
#define numberOfMarkerPlotSymbols 2
#define oneDayInterval 86400 //60*60*24
#define initNumberOfDaysDisplayed 183
#define kRepeat 20 //repeat count, used for animation
#define kFrameRate 20  // frames per second, used for animation



@implementation EskLinePlot

@synthesize delegate;
@synthesize currentBalanceData = _currentBalanceData;
@synthesize years = _years ;
@synthesize belowNinetyDaysDelinquentData = _belowNinetyDaysDelinquentData;
@synthesize aboveNinetyDaysDelinquentData = _aboveNinetyDaysDelinquentData;
@synthesize dailyBalances = _dailyBalances;
@synthesize dailyKeys = _dailyKeys;

- (id)init
{
    self = [super init];
    if (self) 
    {
        //prepare initial display of the balance chart.
        //default to display the data of a year till today
        
        //following properties contain the entire sample data and values would not change
        BalanceModel *balance = [[BalanceModel alloc]init];
        _dailyBalances = balance.dailyBalances;
        _dailyKeys = [_dailyBalances allKeys];
        numberOfPeriods = [_dailyKeys count];
        
        //for animation: to keep a copy of the original balances as the animation will change the balances
        //copyOfDailyBalances = [[NSMutableDictionary alloc]initWithDictionary:balance.dailyBalances];
    
        //get the initial start date: 6 months ago
        NSDate * today = [[NSDate alloc]init];
        initStartDate = [today dateByAddingTimeInterval:(oneDayInterval *initNumberOfDaysDisplayed * -1)];
        NSDateFormatter *ceiFormatter = [[NSDateFormatter alloc] init];
        [ceiFormatter setDateFormat:@"yyyyMMdd"];
        initStartDateString = [ceiFormatter stringFromDate:initStartDate];

            
        NSArray *sortedDailyKeys = [_dailyKeys sortedArrayUsingSelector:@selector(compare:)];
        _dailyKeys = sortedDailyKeys;
        NSInteger initStartIndex = [sortedDailyKeys indexOfObject:initStartDateString];
        
       
        //prepare a new keys array containing only the past year
        NSMutableArray *initialDailyKeys = [NSMutableArray arrayWithCapacity:2000]; 
        for (int i=initStartIndex; i <numberOfPeriods; i++){
            [initialDailyKeys addObject:[sortedDailyKeys objectAtIndex:i]]; 

        }
        
        /*
        for (int i=initStartIndex;i<todayIndex+1;i++){
            [initialDailyKeys addObject:[sortedDailyKeys objectAtIndex:i]]; 
            //NSLog(@"i= %i, init keys = %@", i,[sortedDailyKeys objectAtIndex:i]);
        };*/
              
        //following instance variables represent the current displayed data
        selectedDailyKeys = initialDailyKeys;
        selectedNumberOfPeriods = [selectedDailyKeys count];
        //initial position of ruler; 
        selectedCoordination = 1; //today
        rulerIndex = selectedNumberOfPeriods - selectedCoordination;
        //rulerIndex = selectedNumberOfPeriods/2;//index position is relative
        rulerDateString = [selectedDailyKeys objectAtIndex:rulerIndex];
        
        rangeChanged = NO;
        numberOfDaysInRange = 0;
        rulerEnabled = NO;
        timerCount = 0;
        
              
        
    }
    
    //rangePlotMoved = NO;
    //rulerPlotSelected = NO;
    
    return self;
}


- (void)renderInLayer:(CPTGraphHostingView *)layerHostingView withTheme:(CPTTheme *)theme
{
    
    CGRect bounds = layerHostingView.bounds;
    
    // Create the graph and assign the hosting view.
    graph = [[CPTXYGraph alloc] initWithFrame:bounds];
    layerHostingView.hostedGraph = graph;
    layerHostingView.backgroundColor = [UIColor colorWithWhite:0.7f alpha:1.0f]; 
    
    CPTGradient *plotAreaGradient = [CPTGradient gradientWithBeginningColor:[CPTColor colorWithGenericGray:0.9f]endingColor:[CPTColor colorWithGenericGray:0.7f]];
    plotAreaGradient.angle = -90.0;
    graph.plotAreaFrame.plotArea.fill = [CPTFill fillWithGradient:plotAreaGradient]; 
    
    [graph applyTheme:theme];
    graph.plotAreaFrame.masksToBorder = NO;
  
    
    // change the chart layer orders so the axis line is on top of the bar in the chart.
    NSArray *chartLayers = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:CPTGraphLayerTypePlots],
                            [NSNumber numberWithInt:CPTGraphLayerTypeMajorGridLines], 
                            [NSNumber numberWithInt:CPTGraphLayerTypeMinorGridLines],  
                            [NSNumber numberWithInt:CPTGraphLayerTypeAxisLines], 
                            [NSNumber numberWithInt:CPTGraphLayerTypeAxisLabels], 
                            [NSNumber numberWithInt:CPTGraphLayerTypeAxisTitles], 
                            nil];
    graph.topDownLayerOrder = chartLayers;    
    
    
    // Add plot space for horizontal bar charts
    graph.paddingLeft = 90.0;
	graph.paddingTop = 100.0;//50.0
	graph.paddingRight = 50.0;
	graph.paddingBottom = 25.0;//60.0
    
    CPTMutableTextStyle *blackTextStyle = [[CPTMutableTextStyle alloc] init];
	//blackTextStyle.color = [CPTColor blackColor];
    blackTextStyle.color = [CPTColor colorWithComponentRed:0.2 green:0.2 blue:0.2 alpha:0.82]; //RGB:R51-G51-B51
	blackTextStyle.fontSize = 20.0;
    graph.titleTextStyle = blackTextStyle;
    graph.titleDisplacement = CGPointMake(0.0, 70.0); 
    graph.title = @"Receivables Aging Balance";
        
    // Setup plot space
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
    plotSpace.allowsUserInteraction = YES;
    plotSpace.delegate = self;
 
    //display last 31 days starting today
    plotSpace.xRange=[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(oneDayInterval*0) 
                                                  length:CPTDecimalFromFloat(oneDayInterval*(initNumberOfDaysDisplayed-1))];
    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(100000)];
    
    //set globalXrange same as xRange preventing from horizontal scrolling
    /*plotSpace.globalXRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f)
                              length:CPTDecimalFromFloat(oneDayInterval*initNumberOfDaysDisplayed)];*/
    
    // Setup grid line style
    CPTMutableLineStyle *majorXGridLineStyle = [CPTMutableLineStyle lineStyle];
    majorXGridLineStyle.lineWidth = 1.0f;
    majorXGridLineStyle.lineColor = [[CPTColor grayColor] colorWithAlphaComponent:0.25f];
    
    // Setup x-Axis.
	CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
    CPTXYAxis *x = axisSet.xAxis;
    x.majorGridLineStyle = majorXGridLineStyle;
    x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0");
      
    //setup x axis label based on weeks; one label per 2 months

    x.majorIntervalLength = CPTDecimalFromFloat(oneDayInterval*30);
    x.minorTicksPerInterval = 0;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
 
    dateFormatter.dateStyle = kCFDateFormatterShortStyle; // format is mm/dd/yy
    CPTTimeFormatter *timeFormatter =[[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter];
       
    timeFormatter.referenceDate = initStartDate;
    x.labelFormatter = timeFormatter;

    
    // Setup y-Axis.
    CPTMutableLineStyle *majorYGridLineStyle = [CPTMutableLineStyle lineStyle];
    majorYGridLineStyle.lineWidth = 1.0f;
    majorYGridLineStyle.dashPattern =  [NSArray arrayWithObjects:[NSNumber numberWithFloat:5.0f], [NSNumber numberWithFloat:5.0f], nil];
    majorYGridLineStyle.lineColor = [[CPTColor lightGrayColor] colorWithAlphaComponent:0.25];
    
    
    CPTXYAxis *y = axisSet.yAxis;
    
    y.majorGridLineStyle = majorYGridLineStyle;
    y.majorIntervalLength = CPTDecimalFromString(@"20000");
    y.minorTicksPerInterval = 1;
    y.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0");
    y.title = @"Balance";
    NSArray *yExlusionRanges = [NSArray arrayWithObjects:
                                [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat(0.0)],
                                nil];
    y.labelExclusionRanges = yExlusionRanges;

    
    
    
    //y-axis stays in fixed position
    axisSet.yAxis.axisConstraints = [CPTConstraints constraintWithLowerOffset:0];
    
    
    // Create 1st plot area in Blue for balance
    balancePlot = [[CPTScatterPlot alloc] init];
    balancePlot.identifier = kBalancePlot;
    
    CPTColor *balanceAreaColor  = [CPTColor colorWithComponentRed:0.078 green:0.529 blue:0.0 alpha:1.0]; //RGB: 20-135-0  (green)
    CPTColor *balanceAreaEndColor  = [CPTColor colorWithComponentRed:0.216 green:0.671 blue:0.004 alpha:1.0]; //RGB: 55-171-1  (green)
    CPTGradient *balanceAreaGradient = [CPTGradient gradientWithBeginningColor:balanceAreaColor endingColor:balanceAreaEndColor];
    //CPTGradient *balanceAreaGradient = [CPTGradient gradientWithBeginningColor:balanceAreaColor endingColor:balanceAreaColor];
    balanceAreaGradient.angle = -90.0;
    CPTFill *balanceAreaGradientFill = [CPTFill fillWithGradient:balanceAreaGradient];
    balancePlot.areaFill = balanceAreaGradientFill;
    
    CPTMutableLineStyle *balanceLineStyle = [balancePlot.dataLineStyle mutableCopy];
	balanceLineStyle.lineWidth = 2.f;
    balanceLineStyle.lineColor = balanceAreaColor;
    //balanceLineStyle.lineColor = [CPTColor clearColor];
    balancePlot.dataLineStyle = balanceLineStyle;
    balancePlot.dataSource = self;  
    
    
   
    balancePlot.areaBaseValue = CPTDecimalFromString(@"0");
 
    [graph addPlot:balancePlot];
       
    // Create 2nd plot area in Green for 1-90 Day delinquent
	belowPlot = [[CPTScatterPlot alloc] init];
    belowPlot.identifier = kBelowNinetyPlot;
    
    CPTColor *belowAreaColor = [CPTColor colorWithComponentRed:0.051 green:0.388 blue:0.894 alpha:1.0]; //RGB: 13-99-228  (blue)
    CPTColor *belowAreaEndColor = [CPTColor colorWithComponentRed:0.129 green:0.545 blue:0.894 alpha:1.0];//RGB: 33-139-228  (blue)
    
    CPTGradient *belowAreaGradient = [CPTGradient gradientWithBeginningColor:belowAreaColor endingColor:belowAreaEndColor];
    //CPTGradient *belowAreaGradient = [CPTGradient gradientWithBeginningColor:belowAreaColor endingColor:belowAreaColor];
    belowAreaGradient.angle = -90.0;
    CPTFill *belowAreaGradientFill = [CPTFill fillWithGradient:belowAreaGradient];
    belowPlot.areaFill = belowAreaGradientFill;
    
    CPTMutableLineStyle *belowLineStyle = [belowPlot.dataLineStyle mutableCopy];
	belowLineStyle.lineWidth = 2.f;
    belowLineStyle.lineColor = belowAreaColor;
    //belowLineStyle.lineColor = [CPTColor clearColor];
    belowPlot.dataLineStyle = belowLineStyle;
    belowPlot.dataSource = self;
	    
    
    belowPlot.areaBaseValue = CPTDecimalFromString(@"0");    
    [graph addPlot:belowPlot];
    
    
    // Create 3rd plot area in Red for 90+ day delinquent
    
	abovePlot = [[CPTScatterPlot alloc] init];
    abovePlot.identifier = kAboveNinetyPlot;
    
    CPTColor *aboveAreaColor = [CPTColor colorWithComponentRed:0.882 green:0.251 blue:0.141 alpha:1.0];   //RGB: 225-64-36  (orange)
    CPTColor *aboveAreaEndColor = [CPTColor colorWithComponentRed:0.922 green:0.361 blue:0.125 alpha:1.0]; //RGB: 235-92-32  (orange)
    CPTGradient *aboveAreaGradient = [CPTGradient gradientWithBeginningColor:aboveAreaColor endingColor:aboveAreaEndColor];
    //CPTGradient *aboveAreaGradient = [CPTGradient gradientWithBeginningColor:aboveAreaColor endingColor:aboveAreaColor];
    aboveAreaGradient.angle = -90.0;
    CPTFill *aboveAreaGradientFill = [CPTFill fillWithGradient:aboveAreaGradient];
    abovePlot.areaFill = aboveAreaGradientFill;
    
    CPTMutableLineStyle *aboveLineStyle = [abovePlot.dataLineStyle mutableCopy];
	aboveLineStyle.lineWidth = 2.f;
    aboveLineStyle.lineColor = aboveAreaColor;
    //aboveLineStyle.lineColor = [CPTColor clearColor];
    abovePlot.dataLineStyle = aboveLineStyle;
    abovePlot.dataSource = self;

   
    abovePlot.areaBaseValue = CPTDecimalFromString(@"0");    
    [graph addPlot:abovePlot];
    
    
    //legend for the plotsf
    balancePlot.title = @"Receivables Aging Balance";
    belowPlot.title = @"1-90 Days Delinquent";
    abovePlot.title = @"90+ Days Delinquent";
    CPTLegend *lineLegend = [CPTLegend legendWithPlots:[NSArray arrayWithObjects:balancePlot, belowPlot, abovePlot, nil]]; 
    lineLegend.numberOfColumns = 3;
    graph.legend= lineLegend;
    //graph.legend.fill = [CPTFill fillWithColor:[CPTColor clearColor]];
    graph.legend.cornerRadius = 5.0;
    //graph.legend.swatchCornerRadius = 5.0f;
    //graph.legend.swatchSize = CGSizeMake(10.0, 30.0);
    graph.legend.swatchSize = CGSizeZero;
    graph.legendAnchor = CPTRectAnchorTop;  //CPTRectAnchorRight
    graph.legendDisplacement = CGPointMake(0.0, -50.0);  //position of the legend
    
    CPTMutableTextStyle *legendTextStyle = [[CPTMutableTextStyle alloc] init];
	legendTextStyle.color = [CPTColor darkGrayColor];
	legendTextStyle.fontSize = 11.0;
    legendTextStyle.fontName = @"Helvetica-Bold";
    graph.legend.textStyle = legendTextStyle;
   
    
    // Create the Ruler Plot
    rulerPlot = [[CPTScatterPlot alloc] initWithFrame:CGRectNull];
    rulerPlot.identifier = kRulerPlot;
    rulerPlot.dataSource = self;
    rulerPlot.delegate = self;
    /*
    [self applyRulerPlotColor];
    [self displayRulerAnnotation];  
    [graph addPlot:rulerPlot];*/
   
    /*    
    CABasicAnimation *scaling = [CABasicAnimation animationWithKeyPath:@"transform.scale.y"]; 
    // scaling only on the yaxis
    scaling.fromValue = [NSNumber numberWithFloat:0.0];
    scaling.toValue = [NSNumber numberWithFloat:1.0];
    scaling.duration = 2.0f; // Duration
    scaling.removedOnCompletion = NO;
    scaling.fillMode = kCAFillModeForwards;
    
    
    // Moved anchor point;initially it was set to center 0.5
    balancePlot.anchorPoint = CGPointMake(0.0, 0.0); 
    [balancePlot addAnimation:scaling forKey:@"scaling"]; 
    
    belowPlot.anchorPoint = CGPointMake(0.0, 0.0); 
    [belowPlot addAnimation:scaling forKey:@"scaling"]; 
    
    abovePlot.anchorPoint = CGPointMake(0.0, 0.0); 
    [abovePlot addAnimation:scaling forKey:@"scaling"]; 
    */
   
}



// Assign different color to the touchable line symbol.
- (void)applyRulerPlotColor
{
    CPTColor *rulerPlotColor = [CPTColor yellowColor];
    
    CPTMutableLineStyle *savingsPlotLineStyle = [CPTMutableLineStyle lineStyle];
    savingsPlotLineStyle.lineColor = rulerPlotColor;
    /*
    CPTPlotSymbol *rulerPlotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
    rulerPlotSymbol.fill = [CPTFill fillWithColor:rulerPlotColor];
    rulerPlotSymbol.lineStyle = savingsPlotLineStyle;
    rulerPlotSymbol.size = CGSizeMake(8.0f, 8.0f); 
    rulerPlot.plotSymbol = rulerPlotSymbol;
     */
    
    CPTMutableLineStyle *touchLineStyle = [CPTMutableLineStyle lineStyle];
    touchLineStyle.dashPattern = [NSArray arrayWithObjects:[NSNumber numberWithFloat:4.0f], nil];

    touchLineStyle.lineColor = [CPTColor cyanColor];
    //touchLineStyle.lineColor = [CPTColor colorWithComponentRed:0.129 green:0.545 blue:0.894 alpha:1.0]; //RGB: 33-139-228  (blue) 
    touchLineStyle.lineWidth = 2.0f;
    
    rulerPlot.dataLineStyle = touchLineStyle;
    
}

// Highlight the touch plot when the user holding tap on the line symbol.
- (void)applyHighLightPlotColor:(CPTScatterPlot *)plot
{
    CPTColor *selectedPlotColor = [CPTColor yellowColor];
    
    CPTMutableLineStyle *symbolLineStyle = [CPTMutableLineStyle lineStyle];
    symbolLineStyle.lineColor = selectedPlotColor;
    /*
    CPTPlotSymbol *plotSymbol = nil;
    plotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
    plotSymbol.fill = [CPTFill fillWithColor:selectedPlotColor];
    plotSymbol.lineStyle = symbolLineStyle;
    plotSymbol.size = CGSizeMake(8.0f, 8.0f); 
    plot.plotSymbol = plotSymbol;
    */
    CPTMutableLineStyle *selectedLineStyle = [CPTMutableLineStyle lineStyle];
    selectedLineStyle.dashPattern = [NSArray arrayWithObjects:[NSNumber numberWithFloat:4.0f], nil];
    selectedLineStyle.lineColor = [CPTColor cyanColor];
    //selectedLineStyle.lineColor =  [CPTColor colorWithComponentRed:0.129 green:0.545 blue:0.894 alpha:1.0]; //RGB: 33-139-228  (blue) 
    selectedLineStyle.lineWidth = 2.0f;
    
    plot.dataLineStyle = selectedLineStyle;
}

-(void)removeRulerAnnotation{
    if ( symbolTextAnnotation ) {
        [graph.plotAreaFrame.plotArea removeAnnotation:symbolTextAnnotation];
        symbolTextAnnotation = nil;
    };

    
}

-(void)displayRulerAnnotation
{
   
    if ( symbolTextAnnotation ) {
        [graph.plotAreaFrame.plotArea removeAnnotation:symbolTextAnnotation];
        symbolTextAnnotation = nil;
    };
   
    
    // Setup a style for the annotation
    CPTMutableTextStyle *hitAnnotationTextStyle = [CPTMutableTextStyle textStyle];
    hitAnnotationTextStyle.color	= [CPTColor cyanColor];
    //hitAnnotationTextStyle.color = [CPTColor colorWithComponentRed:0.129 green:0.545 blue:0.894 alpha:1.0]; //RGB: 33-139-228  (blue) 
    hitAnnotationTextStyle.fontSize = 13.0f;
    hitAnnotationTextStyle.fontName = @"Helvetica-Bold";
      
    NSString *refDateString = [selectedDailyKeys objectAtIndex:0];
    Balance *refDateBal = [_dailyBalances objectForKey: 
                           refDateString];
    Balance *rulerBal = [_dailyBalances objectForKey: 
                          rulerDateString];
       
    NSArray *anchorPoint = [NSArray arrayWithObjects:
                            [NSNumber numberWithFloat: (refDateBal.timeIntervalSinceToday - rulerBal.timeIntervalSinceToday)],
                            [NSNumber numberWithInt:104000],nil];
    
    Balance *selectedRulerBalance = [_dailyBalances objectForKey:rulerDateString];
    NSNumber *openInvoices = nil;
    NSNumber *balanceAmount = selectedRulerBalance.balanceAmount;
    openInvoices = selectedRulerBalance.numberOfOpenInvoices;
    
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [formatter setMaximumFractionDigits:2];
    
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"MM/dd/yyyy"];
    NSString *balDateString = [dateFormat stringFromDate:selectedRulerBalance.balanceDate];
    
    NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
    [currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    
    
    NSString *rulerString = [NSString stringWithFormat:@"  %@ - %@", [currencyFormatter stringFromNumber:balanceAmount], balDateString];
    
    
    
    // Now add the annotation to the plot area
    CPTTextLayer *textLayer = [[CPTTextLayer alloc] initWithText:rulerString style:hitAnnotationTextStyle];
    symbolTextAnnotation = [[CPTPlotSpaceAnnotation alloc] initWithPlotSpace:graph.defaultPlotSpace anchorPlotPoint:anchorPoint];
    //UIImage *popoverImage = [UIImage imageNamed:@"popover.png"];
    //textLayer.backgroundColor = [UIColor colorWithPatternImage:popoverImage].CGColor;
    textLayer.backgroundColor = [UIColor blackColor].CGColor;
    textLayer.frame = CGRectMake(0,0, 158, 18); 
    
    symbolTextAnnotation.contentLayer = textLayer;
    symbolTextAnnotation.displacement = CGPointMake(0.0f, 0.0f);
    [graph.plotAreaFrame.plotArea addAnnotation:symbolTextAnnotation];
    
}



//change the date range responding to the range plot
-(void)linePlot:(EskLinePlot *)plot respondToRangeChangeFromDate:(NSString *)newFromDateString toDate:(NSString *)newToDateString{
    
   // NSLog(@"line plot newFromDateString: %@", newFromDateString);
   // NSLog(@"line plot newToDateString: %@", newToDateString);
    NSInteger startIndex = [_dailyKeys indexOfObject:newFromDateString];
    NSInteger endIndex = [_dailyKeys indexOfObject:newToDateString];


    //NSLog(@"line plot startIndex: %i", startIndex);
    //NSLog(@"line plot endIndex: %i", endIndex);
    
    numberOfDaysInRange = 0;
    numberOfDaysInRange = endIndex - startIndex +1;
    if (numberOfDaysInRange != selectedNumberOfPeriods)
        rangeChanged = YES;
    
    int gap = 1;
    if ((endIndex - startIndex) > initNumberOfDaysDisplayed*1.5) {
        gap = (numberOfDaysInRange/initNumberOfDaysDisplayed)+1;
       // NSLog(@"numberOfDaysInRange: %i, gap=%i", numberOfDaysInRange, gap);

    };
    
    //for animation. Copy dailyKeysInRange as it will be changed soon.
    animationStartDailyKeys = [NSMutableArray arrayWithCapacity:2000]; 
    for (int s=0;s<selectedNumberOfPeriods;s++){
        [animationStartDailyKeys addObject:[selectedDailyKeys objectAtIndex:s]];
    };


    //pick the dates to be displayed          
    NSMutableArray *displayDailyKeys  = [NSMutableArray arrayWithCapacity:5000];
    for (int idx= startIndex; idx < endIndex+1; idx++){
        [displayDailyKeys addObject:[_dailyKeys objectAtIndex:idx]];
    };
     
    //readjust ruler position within the new range
    if (rulerEnabled){
        //if (rulerIndex > ([displayDailyKeys count]-1)){
        rulerIndex = ((rulerIndex * [displayDailyKeys count]) / selectedNumberOfPeriods)-1;
           // NSLog(@"new1 rulerIndex=%i",rulerIndex);
        //}
    }
    //NSLog(@"rulerIndex=%i,[displayDailyKeys count]=%i",rulerIndex,[displayDailyKeys count]);
    //NSLog(@"new2 rulerIndex=%i",rulerIndex);
    
    selectedDailyKeys = displayDailyKeys;
    selectedNumberOfPeriods = [selectedDailyKeys count];
    //NSLog(@"selectedNumberOfPeriods = %i",selectedNumberOfPeriods);
    
    //set up animation data
    [self prepareAnimationData];
   
    if (rulerEnabled){
        rulerDateString = [selectedDailyKeys objectAtIndex:rulerIndex];
        [self displayRulerAnnotation];
       
        //aging bars changed accordingly
        if ([delegate respondsToSelector:@selector(linePlot:selectedDate:)]) 
            [delegate linePlot:self selectedDate:rulerDateString];        
    }
       
    // change x-axis range. The range = total number of days within the selected range though only a portion of days is displayed.
    
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(oneDayInterval*0) 
                                                    length:CPTDecimalFromFloat(oneDayInterval* (numberOfDaysInRange-1))];
    
    CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
    CPTXYAxis *x = axisSet.xAxis;
    
    x.majorIntervalLength = CPTDecimalFromFloat(oneDayInterval*30.0 *gap);
    
    NSDateFormatter *fromDateFormatter = [[NSDateFormatter alloc] init];
    [fromDateFormatter setDateFormat:@"yyyyMMdd"];
    NSDate *newFromDate = [fromDateFormatter dateFromString:newFromDateString]; 
    NSDate *refDate = newFromDate;   
    
    NSDateFormatter *refDateFormatter = [[NSDateFormatter alloc] init];
    [refDateFormatter setDateFormat:@"MM/dd/yy"];
    CPTTimeFormatter *timeFormatter =[[CPTTimeFormatter alloc] initWithDateFormatter:refDateFormatter];
    timeFormatter.referenceDate = refDate;
    x.labelFormatter = timeFormatter;
      
    //[graph reloadData];  //uncomment this if no animation
     
    dataTimer = [NSTimer timerWithTimeInterval:0.5/kFrameRate
										 target:self
									   selector:@selector(newData:)
									   userInfo:nil
										repeats:YES];
	[[NSRunLoop mainRunLoop] addTimer:dataTimer forMode:NSDefaultRunLoopMode];
    [rulerPlot reloadData];

    
  };

-(void)prepareAnimationData{
    
    //set up unit of change for animation
    animationUnitBal = [NSMutableArray arrayWithCapacity:2000]; 
    for (int r=0;r<selectedNumberOfPeriods;r++){
       
        Balance *startBal;
        Balance *endBal;
        float newBalAmount,newAboveNinetyDaysAmount,newBelowNinetyDaysAmount;
        float oldBalAmount,oldAboveNinetyDaysAmount,oldBelowNinetyDaysAmount;
        float diffBalAmount,diffAboveNinetyDaysAmount,diffBelowNinetyDaysAmount;
        float unitBalAmount,unitAboveNinetyDaysAmount,unitBelowNinetyDaysAmount;
        
               
        if (r < [animationStartDailyKeys count])
            startBal = [_dailyBalances objectForKey:[animationStartDailyKeys objectAtIndex:r]];
        else 
            startBal=nil;
        
        if (r < [selectedDailyKeys count])
            endBal = [_dailyBalances objectForKey:[selectedDailyKeys objectAtIndex:r]];
        else
            endBal = nil;
        
       
        NSMutableDictionary *unit = [[NSMutableDictionary alloc] init];
        if (endBal){
            newBalAmount = [endBal.balanceAmount floatValue];
            newAboveNinetyDaysAmount = [endBal.aboveNinetyDaysAmount floatValue];
            newBelowNinetyDaysAmount = [endBal.belowNinetyDaysAmount floatValue];
        } else{
            newBalAmount = 0;
            newAboveNinetyDaysAmount = 0;
            newBelowNinetyDaysAmount = 0;
        };
        
        if (startBal){
            oldBalAmount = [startBal.balanceAmount intValue];
            oldAboveNinetyDaysAmount = [startBal.aboveNinetyDaysAmount floatValue];
            oldBelowNinetyDaysAmount = [startBal.belowNinetyDaysAmount floatValue];
        }else{
            oldBalAmount = 0;
            oldAboveNinetyDaysAmount = 0;
            oldBelowNinetyDaysAmount = 0;
        };
        
        //if ((newBalAmount != 0) && (oldBalAmount != 0))
         if ((endBal) && (startBal))    
            diffBalAmount = newBalAmount - oldBalAmount;
        else diffBalAmount = 0;
        //if ((newAboveNinetyDaysAmount != 0) && (oldAboveNinetyDaysAmount != 0))
        if ((endBal) && (startBal))                diffAboveNinetyDaysAmount = newAboveNinetyDaysAmount - oldAboveNinetyDaysAmount;
        else diffAboveNinetyDaysAmount = 0;
        //if ((newBelowNinetyDaysAmount != 0) && (oldBelowNinetyDaysAmount != 0))
        if ((endBal) && (startBal))    
            diffBelowNinetyDaysAmount = newBelowNinetyDaysAmount - oldBelowNinetyDaysAmount;
        else diffBelowNinetyDaysAmount = 0;
        
        unitBalAmount = diffBalAmount / kRepeat;
        unitAboveNinetyDaysAmount = diffAboveNinetyDaysAmount / kRepeat;
        unitBelowNinetyDaysAmount = diffBelowNinetyDaysAmount / kRepeat;
        
        /*if (r == (selectedNumberOfPeriods-1)){
            NSLog(@"prep index=%i,oldBalAmount=%f,unit=%f,newBalAmount=%f",r,oldBalAmount,unitBalAmount,newBalAmount);
        }*/
        
        [unit setObject:[NSNumber numberWithFloat:unitBalAmount] forKey:@"unitBalAmount"];
        [unit setObject:[NSNumber numberWithFloat:unitAboveNinetyDaysAmount] forKey:@"unitAboveNinetyDaysAmount"];
        [unit setObject:[NSNumber numberWithFloat:unitBelowNinetyDaysAmount] forKey:@"unitBelowNinetyDaysAmount"];
     
        [animationUnitBal addObject:unit];
       
    }

}

-(void)newData:(NSTimer *)theTimer
{
        
    /*for (int i=0; i<selectedNumberOfPeriods; i++){
        
        Balance *startBal;
        Balance *endBal;
        NSString *endBalKey;
        
               
        int oldBalAmount,oldAboveNinetyDaysAmount,oldBelowNinetyDaysAmount;
        float unitBalAmount,unitAboveNinetyDaysAmount,unitBelowNinetyDaysAmount;
        
        if (i < [animationStartDailyKeys count])
            startBal = [_dailyBalances objectForKey:[animationStartDailyKeys objectAtIndex:i]];
        else 
            startBal=nil;
        
       if (i < [selectedDailyKeys count]){
            endBalKey = [selectedDailyKeys objectAtIndex:i];
            endBal = [_dailyBalances objectForKey:endBalKey];
        }
        else
            endBal = nil;
        
        if (startBal){
            oldBalAmount = [startBal.balanceAmount intValue];
            oldAboveNinetyDaysAmount = [startBal.aboveNinetyDaysAmount intValue];
            oldBelowNinetyDaysAmount = [startBal.belowNinetyDaysAmount intValue];
        }
        
        if (i < [animationStartDailyKeys count]){
            unitBalAmount = [[[animationUnitBal objectAtIndex:i] objectForKey:@"unitBalAmount"] floatValue];
            unitAboveNinetyDaysAmount = [[[animationUnitBal objectAtIndex:i] objectForKey:@"unitAboveNinetyDaysAmount"] floatValue];
            unitBelowNinetyDaysAmount = [[[animationUnitBal objectAtIndex:i] objectForKey:@"unitBelowNinetyDaysAmount"] floatValue];
        }

        int intrimBalAmount = oldBalAmount + unitBalAmount * (timerCount+1);
        int intrimAboveNinetyDaysAmount = oldAboveNinetyDaysAmount + unitAboveNinetyDaysAmount * (timerCount+1);
        int intrimBelowNinetyDaysAmount = oldBelowNinetyDaysAmount +unitBelowNinetyDaysAmount * (timerCount+1);
                 
        //????this is not entirely correct as I need to change the bucket amounts instead
        if (endBal){
            endBal.balanceAmount = [NSNumber numberWithInt:intrimBalAmount];
            endBal.aboveNinetyDaysAmount=[NSNumber numberWithInt:intrimAboveNinetyDaysAmount];
            endBal.belowNinetyDaysAmount=[NSNumber numberWithInt:intrimBelowNinetyDaysAmount];
            [_dailyBalances removeObjectForKey:endBalKey];
            [_dailyBalances setObject:endBal forKey:endBalKey];
        }
    }*/
    

    
    [balancePlot reloadDataInIndexRange:NSMakeRange(0,selectedNumberOfPeriods)];
    [belowPlot reloadDataInIndexRange:NSMakeRange(0,selectedNumberOfPeriods)];
    [abovePlot reloadDataInIndexRange:NSMakeRange(0,selectedNumberOfPeriods)];
    
    timerCount++;
    if (timerCount >= kRepeat) {
        [theTimer invalidate];
        timerCount = 0;
    }

}


#pragma mark - CPTPlotSpace Delegate Methods
// This implementation of this method will put the line graph in a fix position so it won't be scrollable.

-(CPTPlotRange *)plotSpace:(CPTPlotSpace *)space willChangePlotRangeTo:(CPTPlotRange *)newRange forCoordinate:(CPTCoordinate)coordinate 
{
    
    if (coordinate == CPTCoordinateY) {
        return ((CPTXYPlotSpace *)space).yRange;
    }
    else
    {   
        //return newRange; //allow horizontal scrolling
        return ((CPTXYPlotSpace *)space).xRange;  //disable horizontal scrolling
    }
}

/*
//disable vertical scrolling
-(CGPoint)plotSpace:(CPTPlotSpace *)space willDisplaceBy:(CGPoint)displacement {
    return CGPointMake(displacement.x, 0);
}
*/

// This method is call when user touch & drag the ruler plot
- (BOOL)plotSpace:(CPTPlotSpace *)space shouldHandlePointingDeviceDraggedEvent:(id)event atPoint:(CGPoint)point
{
    
    if (!rulerEnabled){
        [self applyRulerPlotColor];
        [self displayRulerAnnotation]; 
        rulerEnabled = YES;
        [graph addPlot:rulerPlot];
        [rulerPlot reloadData];
    }
     
    
    
    // Convert the touch point to plot area frame location
    CGPoint pointInPlotArea = [graph convertPoint:point toLayer:graph.plotAreaFrame];
    NSDecimal newPoint[2];
    [graph.defaultPlotSpace plotPoint:newPoint forPlotAreaViewPoint:pointInPlotArea];
    NSDecimalRound(&newPoint[0], &newPoint[0], 0, NSRoundPlain);
       
    int x = [[NSDecimalNumber decimalNumberWithDecimal:newPoint[0]] intValue];
          
    //int maxRange = initNumberOfDaysDisplayed;
    int maxRange = selectedNumberOfPeriods;
    
    if ((rangeChanged) && (numberOfDaysInRange > initNumberOfDaysDisplayed) && (numberOfDaysInRange >0)) 
        maxRange = numberOfDaysInRange; 
    
    int totalRange = oneDayInterval*maxRange; 
    //NSLog(@"dragged x point=%i",x);
    //NSLog(@"dragged selectedNumberOfPeriods=%i,totalRange=%i",selectedNumberOfPeriods,totalRange);
          
    if (x > totalRange)
    {
        if (rulerIndex <= (selectedNumberOfPeriods-2)){
            rulerIndex = rulerIndex + 1;
        }
    } else if (x < 0){
        if (rulerIndex >=1){
            rulerIndex = rulerIndex - 1;
        }
    }
            
    else {
        //int totalRange = oneDayInterval*maxRange;
        x =  x/oneDayInterval;        
        if (x < selectedNumberOfPeriods){
            rulerIndex = x;
        }

    };
    selectedDateString = [selectedDailyKeys objectAtIndex:rulerIndex];
    rulerDateString = selectedDateString;
    [self applyHighLightPlotColor:rulerPlot];
    [self displayRulerAnnotation];
    [rulerPlot reloadData];

    return YES;
}

- (BOOL)plotSpace:(CPTPlotSpace *)space shouldHandlePointingDeviceDownEvent:(id)event 
          atPoint:(CGPoint)point{
    
    // Convert the touch point to plot area frame location
    CGPoint pointInPlotArea = [graph convertPoint:point toLayer:graph.plotAreaFrame];
    NSDecimal newPoint[2];
    [graph.defaultPlotSpace plotPoint:newPoint forPlotAreaViewPoint:pointInPlotArea];
    NSDecimalRound(&newPoint[0], &newPoint[0], 0, NSRoundPlain);

    int x = [[NSDecimalNumber decimalNumberWithDecimal:newPoint[0]] intValue];
    x =  x/oneDayInterval;
    
    //int y = [[NSDecimalNumber decimalNumberWithDecimal:newPoint[0]] intValue];
    //NSLog(@"ruler in down y = %d", y);
    
    if (rulerEnabled){
        [graph removePlot:rulerPlot];
        [self removeRulerAnnotation];
        rulerEnabled = NO;
        //[graph reloadDataIfNeeded];
    } else{
        rulerIndex = x;
        if (x< selectedNumberOfPeriods)
            rulerDateString = [selectedDailyKeys objectAtIndex:x];
        else
            rulerDateString = [selectedDailyKeys objectAtIndex:(selectedNumberOfPeriods/2)];

        [self applyRulerPlotColor];
        [self displayRulerAnnotation]; 
        rulerEnabled = YES;
        [graph addPlot:rulerPlot];
        [rulerPlot reloadData];
           
    }
     
    
    return YES;
}

- (BOOL)plotSpace:(CPTPlotSpace *)space shouldHandlePointingDeviceUpEvent:(id)event atPoint:(CGPoint)point
{
       
    // Convert the touch point to plot area frame location
    CGPoint pointInPlotArea = [graph convertPoint:point toLayer:graph.plotAreaFrame];
    NSDecimal newPoint[2];
    [graph.defaultPlotSpace plotPoint:newPoint forPlotAreaViewPoint:pointInPlotArea];
    NSDecimalRound(&newPoint[0], &newPoint[0], 0, NSRoundPlain);
    
    int x = [[NSDecimalNumber decimalNumberWithDecimal:newPoint[0]] intValue];
    int maxRange = initNumberOfDaysDisplayed;
    
    if ((rangeChanged) && (numberOfDaysInRange > initNumberOfDaysDisplayed)) 
        maxRange = numberOfDaysInRange; 
    
    int totalRange = oneDayInterval*maxRange; 
    
    if ((x > totalRange) || ( x < 0)){
        return NO;
    }
    else {
   
        int totalRange = oneDayInterval*maxRange;
        x =  x/(totalRange/selectedNumberOfPeriods);
        
        selectedDateString = [selectedDailyKeys objectAtIndex:x];
       if ([delegate respondsToSelector:@selector(linePlot:selectedDate:)]) 
           [delegate linePlot:self selectedDate:selectedDateString];//only changes bar when releasing the selection
      
        rulerDateString = selectedDateString;
        rulerIndex = x;
        [self applyRulerPlotColor];
        return YES;
    };

}

/*
#pragma mark - 
#pragma mark Scatter plot delegate methods

- (void)scatterPlot:(CPTScatterPlot *)plot plotSymbolWasSelectedAtRecordIndex:(NSUInteger)index
{
    
    if ([(NSString *)plot.identifier isEqualToString:kRulerPlot]) 
    {
        //rulerPlotSelected = YES;
        //[self applyHighLightPlotColor:plot];
        //if ([delegate respondsToSelector:@selector(linePlot:selectedDate:)]) 
        //    [delegate linePlot:self selectedDate:selectedDateString];
        
    } 
     
}
*/



#pragma mark -
#pragma mark Plot Data Source Methods

- (NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot 
{
    if ([(NSString *)plot.identifier isEqualToString:kRulerPlot]) 
    {
        return numberOfMarkerPlotSymbols;
    } else
    {
        //NSLog(@"record selectedNumberOfPeriods = %i", selectedNumberOfPeriods);
        return selectedNumberOfPeriods;
    }
}



- (NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index 
{       
    NSNumber *num = nil;
    
    
    
    if (( [(NSString *)plot.identifier isEqualToString:kBalancePlot] ) ||
        ( [(NSString *)plot.identifier isEqualToString:kBelowNinetyPlot] ) ||
        ( [(NSString *)plot.identifier isEqualToString:kAboveNinetyPlot] ))
    {   
        
        NSString *refDateString = [selectedDailyKeys objectAtIndex:0];
        Balance *refDateBal = [_dailyBalances objectForKey:refDateString];
        
        Balance *startBal, *endBal, *bal;
        
        NSString *indexDateString = [selectedDailyKeys objectAtIndex:index];
        endBal = [_dailyBalances objectForKey: indexDateString];
        
        if (index < [animationStartDailyKeys count]){
            NSString *startDateString = [animationStartDailyKeys objectAtIndex:index];
            startBal = [_dailyBalances objectForKey: startDateString];
        } else 
            startBal=nil;
       
        if (startBal) 
            bal = startBal;
        else
            bal = endBal;

        NSDictionary *unit = [animationUnitBal objectAtIndex:index];
        NSNumber *balUnit = [unit objectForKey:@"unitBalAmount"];
        NSNumber *belowUnit = [unit objectForKey:@"unitBelowNinetyDaysAmount"];
        NSNumber *aboveUnit = [unit objectForKey:@"unitAboveNinetyDaysAmount"];
        
        
      
               
        
        if ( fieldEnum == CPTScatterPlotFieldY ) 
        {
            if ([(NSString *)plot.identifier isEqualToString:kBalancePlot])
            {
         
                float balAmount = [bal.balanceAmount floatValue];
                float balanceAmount = balAmount + [balUnit floatValue] * (timerCount+1); 
                num = [NSNumber numberWithFloat:balanceAmount];
                
                /*if (index ==(selectedNumberOfPeriods-1))
                NSLog(@"index=%i,timerCount=%i,origBal=%f,unit=%f,newBal=%f, endBal=%f",index,timerCount,balAmount,[balUnit floatValue],balanceAmount,[endBal.balanceAmount floatValue]);*/
                
            } else if ([(NSString *)plot.identifier isEqualToString:kBelowNinetyPlot])
            { 
                float abvAmount = [bal.aboveNinetyDaysAmount floatValue];
                float belAmount = [bal.belowNinetyDaysAmount floatValue];
                float belowAmount = belAmount + [belowUnit floatValue] * (timerCount+1) +
                    abvAmount + [aboveUnit floatValue] * (timerCount+1); 
                num = [NSNumber numberWithFloat:belowAmount];
                         
                //int belAmount = [bal.belowNinetyDaysAmount intValue] + [bal.aboveNinetyDaysAmount intValue];
                //num = [NSNumber numberWithInt:bAmount];
                
                
            } else if ([(NSString *)plot.identifier isEqualToString:kAboveNinetyPlot])
            { 
                float abvAmount = [bal.aboveNinetyDaysAmount floatValue];
                float aboveAmount = abvAmount + [aboveUnit floatValue] * (timerCount+1); 
                num = [NSNumber numberWithFloat:aboveAmount];
                //num = bal.aboveNinetyDaysAmount;
            }
            
        } 
        else if (fieldEnum == CPTScatterPlotFieldX) 
        {   
            //num=[NSNumber numberWithFloat: (index * oneDayInterval)];
            num=[NSNumber numberWithFloat: (refDateBal.timeIntervalSinceToday - endBal.timeIntervalSinceToday)];
            
        }
    }  else if ([(NSString *)plot.identifier isEqualToString:kRulerPlot]) 
    {
        
        if ( fieldEnum == CPTScatterPlotFieldY ) 
        {
            switch (index) {
                case 0:
                    num = [NSNumber numberWithInt:0];
                    break; 
                case 1:
                    num = [NSNumber numberWithInt:99000];
                    break;    
                default:
                    num = [NSNumber numberWithInt:0];
                    break;
            }
        } 
        else if (fieldEnum == CPTScatterPlotFieldX) 
        {   
            NSString *refDateString = [selectedDailyKeys objectAtIndex:0];
            Balance *refDateBal = [_dailyBalances objectForKey: 
                                   refDateString];
            Balance *rulerBal = [_dailyBalances objectForKey: 
                                 rulerDateString];
            
            num=[NSNumber numberWithFloat: (refDateBal.timeIntervalSinceToday - rulerBal.timeIntervalSinceToday)];
        }
    }
    
    return num;
}






/*
- (NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index 
{       
    NSNumber *num = nil;
    
    
    
    if (( [(NSString *)plot.identifier isEqualToString:kBalancePlot] ) ||
        ( [(NSString *)plot.identifier isEqualToString:kBelowNinetyPlot] ) ||
        ( [(NSString *)plot.identifier isEqualToString:kAboveNinetyPlot] ))
    {   
        
        NSString *refDateString = [selectedDailyKeys objectAtIndex:0];
        Balance *refDateBal = [_dailyBalances objectForKey: 
                               refDateString];
        NSString *indexDateString = [selectedDailyKeys objectAtIndex:index];
        Balance *bal = [_dailyBalances objectForKey: 
                        indexDateString];
            
        if ( fieldEnum == CPTScatterPlotFieldY ) 
        {
            if ([(NSString *)plot.identifier isEqualToString:kBalancePlot])
            {
                //Since it's a stacked amount, essentially, the above addition = balanceAmount
                int balAmount = [bal.balanceAmount intValue];
                num = [NSNumber numberWithInt:balAmount];
                
            } else if ([(NSString *)plot.identifier isEqualToString:kBelowNinetyPlot])
            { 
                int bAmount = [bal.belowNinetyDaysAmount intValue] + [bal.aboveNinetyDaysAmount intValue];
                num = [NSNumber numberWithInt:bAmount];
                      
                
            } else if ([(NSString *)plot.identifier isEqualToString:kAboveNinetyPlot])
            { 
                num = bal.aboveNinetyDaysAmount;
            }
         
        } 
        else if (fieldEnum == CPTScatterPlotFieldX) 
        {   
            //num=[NSNumber numberWithFloat: (index * oneDayInterval)];
            num=[NSNumber numberWithFloat: (refDateBal.timeIntervalSinceToday - bal.timeIntervalSinceToday)];
       
        }
    }  else if ([(NSString *)plot.identifier isEqualToString:kRulerPlot]) 
    {
             
        if ( fieldEnum == CPTScatterPlotFieldY ) 
        {
            switch (index) {
                case 0:
                    num = [NSNumber numberWithInt:0];
                    break; 
                case 1:
                    num = [NSNumber numberWithInt:99000];
                    break;    
                default:
                    num = [NSNumber numberWithInt:0];
                    break;
            }
        } 
        else if (fieldEnum == CPTScatterPlotFieldX) 
        {   
            NSString *refDateString = [selectedDailyKeys objectAtIndex:0];
            Balance *refDateBal = [_dailyBalances objectForKey: 
                                   refDateString];
            Balance *rulerBal = [_dailyBalances objectForKey: 
                                  rulerDateString];

            num=[NSNumber numberWithFloat: (refDateBal.timeIntervalSinceToday - rulerBal.timeIntervalSinceToday)];
            }
    }
    
    return num;
}*/
/*
#pragma mark - CPScatterPlotDataSource Delegate Methods
- (CPTPlotSymbol *) symbolForScatterPlot:(CPTScatterPlot *) plot recordIndex:(NSUInteger)index 
{  
    CPTPlotSymbol *rulerPlotSymbol = nil;
    CPTColor *rulerPlotColor = [CPTColor yellowColor];
    CPTMutableLineStyle *savingsPlotLineStyle = [CPTMutableLineStyle lineStyle];
    savingsPlotLineStyle.lineColor = rulerPlotColor;
    
    if (plot == rulerPlot && index == 4) 
    {   
        CPTPlotSymbol *rulerPlotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
        rulerPlotSymbol.fill = [CPTFill fillWithColor:rulerPlotColor];
        rulerPlotSymbol.lineStyle = savingsPlotLineStyle;
        rulerPlotSymbol.size = CGSizeMake(8.0f, 8.0f); 
    }
    
    return rulerPlotSymbol;   
    
};
*/

@end
